package fule.com.webviewdemo.fragment;

import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.Html;
import android.text.method.ScrollingMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.concurrent.ExecutionException;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import fule.com.webviewdemo.R;
import fule.com.webviewdemo.util.ScreenUtil;

/**
 * 作者： njb
 * 时间： 2018/8/31 0031-下午 5:08
 * 描述：
 * 来源：
 */
public class FM_ProductCase extends Fragment {
    @BindView(R.id.tv_detail)
    TextView tvDetail;
    Unbinder unbinder;
    private View rootView;
    private Drawable drawable;
    private String content;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = inflater.inflate(R.layout.activity_text_html, container, false);
        }
        ViewGroup parent = (ViewGroup) rootView.getParent();
        if (parent != null) {
            parent.removeView(rootView);
        }
        unbinder = ButterKnife.bind(this, rootView);
        initView();
        return rootView;

    }

    private void initView() {
        content = "<img src=\"https://www.38.hn/images/image/20161104/20161104144203_82117.jpg\" alt=\"\" /><img src=\"https://www.38.hn/images/image/20161116/20161116165842_88829.jpg\" alt=\"\" /><img src=\"https://www.38.hn/images/image/20161104/20161104144206_17003.jpg\" alt=\"\" /><img src=\"https://www.38.hn/images/image/20161104/20161104144207_62404.jpg\" alt=\"\" /><img src=\"https://www.38.hn/images/image/20161104/20161104144208_78881.jpg\" alt=\"\" />";
        setHtml(content);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    /**
     * 设置html
     *
     * @param content
     */
    private void setHtml(String content) {
        tvDetail.setMovementMethod(ScrollingMovementMethod.getInstance());
        if (Build.VERSION.SDK_INT >= 24) {
            tvDetail.setText(Html.fromHtml(content, Html.FROM_HTML_MODE_COMPACT, imageGetter, null));
        } else {
            tvDetail.setText(Html.fromHtml(content, imageGetter, null));
        }
    }

    Html.ImageGetter imageGetter = new Html.ImageGetter() {
        @Override
        public Drawable getDrawable(String source) {
            if (drawable != null) {
                return drawable;
            } else {
                initDrawable(source);
            }
            return null;
        }
    };

    /**
     * 加载网络图片
     *
     * @param s
     */
    private void initDrawable(final String s) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    final Drawable drawable1 = Glide.with(getActivity()).load(s).submit().get();
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (drawable1 != null) {
                                drawable1.setBounds(0, 0, ScreenUtil.getScreenWidth(getActivity()),ScreenUtil.getFactHeight(getActivity()));
                                //多张图片情况下进行存储：drawableMap.put(s,drawable);
                                drawable = drawable1;
                                setHtml(content);
                            }
                        }
                    });
                } catch (InterruptedException | ExecutionException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}
